package pers.test.votemanage.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.test.votemanage.dao.OptionsMapper;
import pers.test.votemanage.dao.VoteMapper;
import pers.test.votemanage.domain.Options;
import pers.test.votemanage.domain.Vote;
import pers.test.votemanage.service.VoteService;

@Service
public class VoteServiceImpl  implements VoteService {

    @Autowired
    private VoteMapper voteMapper;

    @Autowired
    private OptionsMapper optionsMapper;


    @Override
    public void insertVote(Vote vote) {
        Vote vote1 = Vote.builder().id(vote.getId()).optionsId(vote.getOptionsId())
                .questionId(vote.getQuestionId()).userName(vote.getUserName())
                .userFlag(vote.getUserFlag()).build();
        voteMapper.insertVote(vote1);
        Options options = optionsMapper.selectById(vote1.getOptionsId());
        int count = options.getTotal();
        count++;
        options.setTotal(count);
        optionsMapper.updateCount(options);
    }
}
